package com.ideal.view.form;

import javax.swing.*;

import com.ideal.utils.Empty;
import com.ideal.view.component.TemplateConfiguration;
import com.ideal.view.settings.CustomFileFilter;

import java.awt.event.ActionEvent;
import java.io.File;

/**
 * @author
 */
public class TemplateConfigForm {
    public JPanel rootPanel;
    public JTextField controller;
    public JTextField service;
    public JTextField serviceimpl;
    public JTextField domain;
    public JTextField domain_hbm_xml;
    public JTextField dao;
    public JTextField daoimpl;
    public JButton CONTROLLER;
    public JButton SERVICE;
    public JButton SERVICE_IMPL;
    public JButton DAO;
    public JButton DAO_IMPL;
    public JButton DOMAIN;
    public JButton DOMAIN_HBM_XML;
    public JTextField jdbcUrl;
    public JTextField username;
    public JTextField password;
    public JTextField driverClass;
    public JTextField Form;
    private JButton FORM;
    private CustomFileFilter customFileFilter = new CustomFileFilter();
    public TemplateConfigForm(){
        //设置不可编辑
        driverClass.setText("oracle.jdbc.driver.OracleDriver");
        driverClass.setEditable(false);
        CONTROLLER.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                controller.setText(file.getPath());
            }
        });
        SERVICE.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                service.setText(file.getPath());
            }
        });
        SERVICE_IMPL.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                serviceimpl.setText(file.getPath());
            }
        });
        DAO.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                dao.setText(file.getPath());
            }
        });
        DAO_IMPL.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                daoimpl.setText(file.getPath());
            }
        });
        DOMAIN.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                domain.setText(file.getPath());
            }
        });
        DOMAIN_HBM_XML.addActionListener((ActionEvent e)-> {
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                domain_hbm_xml.setText(file.getPath());
            }
        });
        FORM.addActionListener((ActionEvent e)->{
            File file = getFilePath();
            if(!Empty.isEmpty(file)){
                Form.setText(file.getPath());
            }
        });
    }


    /**
     * 判断是否修改表单值
     * @param data
     * @return
     */
    public boolean isModified(TemplateConfiguration data){
        StringBuffer buffer = new StringBuffer();
        buffer.append(jdbcUrl.getText());
        buffer.append(username.getText());
        buffer.append(password.getText());
        buffer.append(controller.getText());
        buffer.append(service.getText());
        buffer.append(serviceimpl.getText());
        buffer.append(dao.getText());
        buffer.append(daoimpl.getText());
        buffer.append(domain.getText());
        buffer.append(domain_hbm_xml.getText());
        if("".equals(buffer.toString().trim())){
            return false;
        }
        return true;
    }


    public JComponent getRootComponent(){
        this.rootPanel.setBounds(0,0,400,250);
        return this.rootPanel;
    }

    /**
     * 获取选择的文件路径
     * @return
     */
    private File getFilePath() {
        JFileChooser dlg = new JFileChooser();
        dlg.setMultiSelectionEnabled(false);
        dlg.setDialogTitle("请选择模板路径");
        dlg.setFileFilter(customFileFilter);
        // 打开"打开文件"对话框
        dlg.showOpenDialog(null);
        File file = dlg.getSelectedFile();
        return file;
    }
}
































